<?php
namespace app\controller;

use app\BaseController;
use \think\facade\Request;
use \think\facade\Cache;
use \think\facade\Db;
use \think\facade\Log;
use \think\File;
class Excel extends BaseController
{
  //导出
  public function exportData(Request $request)
  {
             $params = Request::param();
        $data = [];
            $dataList1 = Db::name('qrcode_p')
                ->field('qr_sn')
                ->where('bath','like','%'.$params['bath'].'%') //改成模糊查询
                ->select()
                ->all();
            $dataList2 = Db::name('qrcode')
                ->field('qr_sn')
                ->where('bath','like','%'.$params['bath'].'%') //改成模糊查询
                ->select()
                ->all();
            foreach($dataList1 as $item1){
                $data[] = $item1;
            }
            foreach($dataList2 as $item2){
                $data[] = $item2;
            }

            foreach($data as $key=>$item){
                // 获取当前domain
               $domain = Request::domain();
               $data[$key]['url'] = $domain.'/qrController/export?qr_sn='.$item['qr_sn'];
           }
            
        //此处说明：解决数字太长尾数变000的问题
        //由于数字超过15位，会被显示成0或者加小数点处理。造成这种情况是由于Excel内置的数值有效范围是15位。超过15位，如果要显示的话，就需要转换成非数字格式。比如文本格式。
        foreach ($data as $key => $value) {
            $data[$key]['qr_sn'] = "\t".$value['qr_sn']."\t";
            $data[$key]['url'] = "\t".$value['url']."\t";
        }
        //这里规定列名
        return \Excel::export('二维码导出'.time().'.xlsx',['编号','链接'],$data);
 
 }
}